<!--
 * @Description: （享实惠 | 酒慕网）落地页组件
 * @Author: Kerwin
 * @Date: 2024-09-18 11:18:55
 * @LastEditTime: 2025-03-11 17:09:24
 * @LastEditors:  Please set LastEditors
-->
<template>
  <view class="c_container">
    <u-modal v-model="show" async-close blur="5" show-cancel-button @confirm="confirm">
      <view class="slot-content">
        <view class="tit"> 确认绑定 {{ innovateType == 1? '享实惠账号？' : innovateType == 0 ? '关系吗？' : '酒慕网账号？' }} </view>
        <view class="con">邀请码：{{ inviteCode }}</view>
      </view>
    </u-modal>
  </view>
</template>
<script setup lang="ts">
import { userApi } from '@/api';
import { ref, nextTick } from 'vue';
import { useConfigStore } from '@/store';
import { innovate_type,innovate_shopId } from "@/api/enum"

const configStore = useConfigStore()
const props = defineProps({
  innovateType: {
    type: Number,
    default: 1
  },
  shopId: {
    type: Number,
    default: 3125
  },
  inviteCode: {
    type: String,
    default: ''
  },
  userId: {
    type: Number,
    default: 0
  }
})
const show = ref(false)
function open(id: number) {
  setTimeout(() => {
    show.value = true
    userId.value = id
  }, 500)
}
const userId = ref()
async function innovateBind() {
  const shopId = props.shopId || innovate_shopId[props.innovateType as keyof typeof innovate_shopId] || 0; // 添加默认值 0
  const innovateType = props.shopId ? 3 : props.innovateType;

  if (!shopId) {
    console.error('shopId 未正确设置');
    uni.showToast({
      title: 'shopId 未正确设置',
      icon: 'none',
      duration: 3000
    });
    return;
  }
console.log(props.shopId,'得到的props.shopId');

  const res = await userApi.innovateBind({
    userId: userId.value,
    shareCode: props.inviteCode,
    innovateType: innovateType,
    shopId:  props.shopId ?? innovate_shopId[props.innovateType as keyof typeof innovate_shopId] ?? 0
  });

  uni.removeStorageSync('innovateType');
  uni.showToast({
    title: '绑定成功',
    icon: 'none',
    duration: 4000,
    success: () => {
      if (configStore.isWeChatBrowser) {
        setTimeout(() => {
          uni.switchTab({
            url: '/pages/index/index'
          });
        }, 4000);
      }
    }
  });
}

async function confirm() {
  try{
    await innovateBind()
  } catch (err) {
    console.log(err)
  }
  show.value = false
}
defineExpose({
  open
})
</script>


<style lang="scss" scoped>
.slot-content {
  text-align: left;
  padding: 30rpx;
  line-height: 40rpx;

  .tit {
    font-size: 30rpx;
    font-weight: bold;
    color: #333;
    text-align: center;
  }

  .con {
    font-size: 28rpx;
    text-align: center;
    margin-top: 20rpx;
  }
}



.btns {
  display: flex;
  justify-content: space-between;
  padding: 30rpx 0 calc(30rpx + env(safe-area-inset-bottom)) 0;
}
</style>
